package com.example.booksystem.view;

import com.example.booksystem.controller.BookController;
import com.example.booksystem.model.Book;
import com.example.booksystem.model.User;
import com.example.booksystem.view.page.BorrowBook;
import com.example.booksystem.view.page.InsertBook;
import com.example.booksystem.view.page.UserManage;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.sql.SQLException;
import java.util.List;

public class Index {
    private static final String[] ADMIN = {"查找图书","添加图书","用户管理","退出系统"};//管理员
    private static final String[] NORMAL = {"查找图书","已借图书","退出系统"};//用户
    private Stage stage;
    private Scene scene;
    private VBox vBox;
    private VBox data;
    private List<Book> books;
    private User user;
    public Index(Stage stage){
        this.stage = stage;
    }
    public void show(User user){
        this.user = user;
        vBox = new VBox();
        scene = new Scene(vBox,1280,720);
        stage.setScene(scene);
        data = new VBox();
        String type;
        if (user.getUserType() == 0 || user.getUserType() == 1){
            type = "管理员";
        }else{
            type = "普通用户";
        }
        insertButton(user.getUserType());//添加按钮
        insertLabel();//添加标签
        getAllBook();
        showBookList(user.getUserType());
        vBox.getChildren().add(data);
        stage.setTitle("图书管理系统-" + type);
        stage.show();
    }

    private void getAllBook() {
        try {
            books = new BookController().getAllBook();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    private void insertLabel() {
        HBox hBox = new HBox();
        hBox.setAlignment(Pos.CENTER);
        String[] str = {"id","书名","作者","出版社","价格","数量","操作"};
        for (int i = 0; i < 7; i++) {
            HBox box = new HBox();
            box.setAlignment(Pos.CENTER);
            box.setPrefWidth(180);
            Label label = new Label(str[i]);
            box.getChildren().add(label);
            hBox.getChildren().add(box);
        }

        vBox.getChildren().add(hBox);
    }

    private void showBookList(Integer type) {
        ScrollPane scrollPane = new ScrollPane();
        scrollPane.setPrefHeight(680);
        scrollPane.setPrefWidth(1200);
        scrollPane.setFitToWidth(true);
        VBox bookList = new VBox();
        for (int i = 0; i < books.size(); i++) {
            HBox hBox = new HBox();
            hBox.setAlignment(Pos.CENTER);
            Label[] labels = new Label[6];
            labels[0] = new Label(books.get(i).getId());
            labels[1] = new Label(books.get(i).getTitle());
            labels[2] = new Label(books.get(i).getAuthor());
            labels[3] = new Label(books.get(i).getPress());
            labels[4] = new Label(books.get(i).getPrice());
            labels[5] = new Label(String.valueOf(books.get(i).getNum()));
            for (Label label : labels) {
                HBox box = new HBox();
                box.setAlignment(Pos.CENTER);
                box.setPrefWidth(180);
                box.getChildren().add(label);
                hBox.getChildren().add(box);
            }
            bookList.getChildren().add(hBox);
            HBox operate = new HBox();
            operate.setAlignment(Pos.CENTER);
            operate.setPrefWidth(180);
            Book book = books.get(i);
            switch (type) {
                case 0:
                case 1:
                    Button delete = new Button("删除");
                    Button update = new Button("修改");
                    delete.setOnAction(e->{
                        Boolean status = null;
                        try {
                            status = new BookController().delete(book);
                        } catch (SQLException ex) {
                            throw new RuntimeException(ex);
                        }
                        if (status){
                            success("删除成功");
                        }
                    });
                    update.setOnAction(e->{
                        updatePage(book.getId());
                    });
                    operate.getChildren().add(delete);
                    operate.getChildren().add(update);
                    break;
                case 2:
                    Button borrow = new Button("借阅");
                    borrow.setOnAction(e->{
                        try {
                            if (new BookController().borrow(book,user.getId())){
                                success("借阅成功");
                            }else {
                                System.out.println("借阅失败");
                            }
                        } catch (SQLException ex) {
                            throw new RuntimeException(ex);
                        }
                    });
                    operate.getChildren().add(borrow);
                    break;
                default:
                    break;
            }
            hBox.getChildren().add(operate);
        }

        scrollPane.setContent(bookList);
        data.getChildren().add(scrollPane);
    }

    private void insertButton(Integer type) {
        Button[] buttons;
        Integer size;
        switch (type){
            case 0:
            case 1:
                size = ADMIN.length;
                buttons = new Button[size];
                createButton(buttons,ADMIN,size,type);
                adminAction(buttons);
                break;
            case 2:
                size = NORMAL.length;
                buttons = new Button[size];
                createButton(buttons,NORMAL,size,type);
                nomalAction(buttons);
                break;
        }
    }
    private void createButton(Button[] buttons, String[] operate, Integer size,Integer type) {
        HBox hBox = new HBox();
        hBox.setAlignment(Pos.CENTER);
        TextField search = new TextField();
        search.setPromptText("请输入书名、出版社或作者");
        hBox.getChildren().add(search);
        for (int i = 0; i < size; i++) {
            buttons[i] = new Button(operate[i]);
            hBox.getChildren().add(buttons[i]);
        }
        buttons[0].setOnAction(e->{
            //查找图书
            data.getChildren().clear();
            String text = search.getText();
            try {
                books = new BookController().search(text);
                showBookList(type);
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }

        });
        buttons[size-1].setOnAction(e->{
            Login login = new Login(stage);
            login.show();
        });
        vBox.getChildren().add(hBox);
    }

    private void nomalAction(Button[] buttons) {
        buttons[1].setOnAction(e->{
            BorrowBook.show(user.getId());
        });
    }
    private void adminAction(Button[] buttons) {
        //{"添加图书","用户管理"}
        buttons[1].setOnAction(e->{
            InsertBook.show();
        });
        buttons[2].setOnAction(e->{
            UserManage.show(user);
        });

    }
    public static void success(String message){
        Stage stage = new Stage();
        VBox vBox = new VBox();
        HBox hBox1 = new HBox();
        HBox hBox2 = new HBox();
        Label label = new Label(message);
        Button exit = new Button("确定");
        exit.setOnAction(e->{
            stage.close();
        });
        hBox1.setAlignment(Pos.CENTER);
        hBox2.setAlignment(Pos.BASELINE_RIGHT);
        hBox1.getChildren().add(label);
        hBox2.getChildren().add(exit);
        vBox.getChildren().addAll(hBox1,hBox2);
        Scene scene = new Scene(vBox,300,100);
        stage.setTitle("success");
        stage.setScene(scene);
        stage.show();
    }
    private void updatePage(String id) {
        Stage stage = new Stage();
        VBox vBox = new VBox();
        String[] newBook = {"书名:","请输入书名","作者:","请输入作者","出版社:","请输入出版社","价格:","请输入价格"};
        TextField[] textFields = new TextField[4];
        for (int i = 0; i < 4; i++) {
            Label label = new Label(newBook[i*2]);
            textFields[i] = new TextField();
            textFields[i].setPromptText(newBook[i*2+1]);
            HBox box = new HBox();
            box.setAlignment(Pos.CENTER);
            box.getChildren().add(label);
            box.getChildren().add(textFields[i]);
            vBox.getChildren().add(box);
        }
        HBox box = new HBox();
        box.setAlignment(Pos.CENTER);
        Button update = new Button("修改");
        box.getChildren().add(update);
        vBox.getChildren().add(box);
        update.setOnAction(e->{
            Book book = new Book();
            book.setTitle(textFields[0].getText());
            book.setAuthor(textFields[1].getText());
            book.setPress(textFields[2].getText());
            book.setPrice(textFields[3].getText());
            book.setId(id);
            try {
                if (new BookController().update(book)){
                    success("修改成功");
                }
                stage.close();
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
        });
        Scene scene = new Scene(vBox,300,200);
        stage.setTitle("修改图书");
        stage.setScene(scene);
        stage.show();
    }
}
